package com.geekgo.ladder.core.util;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.context.ContextLoader;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * @author geekgo
 * @version 2016/12/21 11:28
 */
public final class InstanceUtil {

    private InstanceUtil(){

    }

    public static final <E> ArrayList<E> newArrayList(){
        return new ArrayList<E>();
    }

    public static final <K> K getBean(Class<K> cls){
        return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
    }

    /**
     * 实例化并复制属性
     * @param origin
     * @param clazz
     * @param <K>
     * @return
     */
    public static final <K> K to(Object origin,Class<K> clazz){
        K bean = null;
        try {
            bean = clazz.newInstance();
            PropertyUtils.copyProperties(bean,origin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
    }
}
